/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import java.util.Vector;
import javax.microedition.lcdui.Graphics;

/**
 *
 * @author dung.voanh
 */
public class MyObject {

    private static MyObject s_myObject;

    public static  MyPlayer s_player;
    public static  Vector s_listEnemy;
    public static  Vector s_listBullet;

    public static MyObject GetInstance() {
        if (s_myObject == null) {
            s_myObject = new MyObject();
        }
        return s_myObject;
    }

    /**
     * Count sum Owner's bullet fired
     *
     * @param owner
     * @return
     */
    public int GetSumBullet(MyActor owner) {
        int count = 0;
        synchronized (s_listBullet) {
            for (int i = 0; i < s_listBullet.size(); i++) {
                MyBullet bullet = (MyBullet) s_listBullet.elementAt(i);
                if (bullet.GetOwner().equals(owner)) {
                    count++;
                }
            }
        }
        return count;
    }

    private MyObject() {
        s_listEnemy = new Vector();
        s_listBullet = new Vector(); 
    }

    public void ClearAll() {
        s_listEnemy.removeAllElements();
        s_listBullet.removeAllElements(); 
    }

    public void Add(MyActor actor) {
        if (actor instanceof MyPlayer) {
            s_player = (MyPlayer) actor;

        } else if (actor instanceof MyBullet) {
            s_listBullet.addElement(actor);

        } else if (actor instanceof MyEnemy || actor instanceof MyEnemyFixed) {
            s_listEnemy.addElement(actor);
        }

    }

    public void Add(int enemyType, int posX, int posY) {
        switch (enemyType) {
            case DEFINE.OBJECT_TYPE_PLAYER: {
                int aiPlayerID[] = new int[]{0};
                s_player = new MyPlayer(aiPlayerID);
                s_player.SetPositionOnMap(posX, posY);
                 if(s_player.m_iXMap > Midlet.s_canGame.getWidth()/2)
                 {
                     GameLayerManager.GetInstance().SetViewWindows(s_player.m_iXMap - Midlet.s_canGame.getWidth()/2, 0);
                 }
                     
            }
            break;

            case DEFINE.OBJECT_TYPE_ENEMY1: {
                int aiEnemyID[] = new int[]{3};
                MyEnemy e = new MyEnemy(aiEnemyID);
                e.SetPositionOnMap(posX, posY);
                s_listEnemy.addElement(e);
            }
            break;

            case DEFINE.OBJECT_TYPE_ENEMY2: {
                int aiEnemy2ID[] = new int[]{4};
                MyEnemyFixed e2 = new MyEnemyFixed(aiEnemy2ID);
                e2.SetPositionOnMap(posX, posY);
                s_listEnemy.addElement(e2);
            }
            break;
        }
    }

    public void Update() {
        GameLayerManager layerManager = GameLayerManager.GetInstance();
        s_player.Update();

        synchronized (s_listBullet) {
            for (int i = 0; i < s_listBullet.size(); i++) {
                MyBullet bullet = (MyBullet) s_listBullet.elementAt(i);
                bullet.Update();

                //check collision with player
                if (bullet.GetOwner().equals(s_player) == false && s_player.ISDie() == false && s_player.Collision(bullet)) {
                    s_player.Die();
                    bullet.Die();
                }
                //Check remove bullet go out window
                if (layerManager.IsOutWindowView(bullet.m_iXMap, bullet.m_iYMap)) { 
                    bullet.Die();
                    //System.out.println("Bullet is out window.");
                }
            }
        }
        synchronized (s_listEnemy) {
            for (int i = 0; i < s_listEnemy.size(); i++) {
                MyActor enemy = (MyActor) s_listEnemy.elementAt(i);
                if (enemy instanceof MyEnemyFixed) {
                    ((MyEnemyFixed) enemy).SetDestinationForBullet(s_player.m_iXMap, s_player.m_iYMap);
                }
                enemy.Update();

                //Check collision with player
                if (s_player.ISDie() == false && s_player.Collision(enemy)) {
                    s_player.Die();
                }

                //Check collision width bullet                
                for (int idBulleet = 0; idBulleet < s_listBullet.size(); idBulleet++) {
                    MyBullet bullet = (MyBullet) s_listBullet.elementAt(idBulleet);                    
                    //Only check Player's bullet
                    if (bullet.GetOwner().equals(s_player) == true && enemy.Collision(bullet)) {
                        enemy.Die();
                        bullet.Die();
                    }
                }
            }

            //Remove all bullet is died
            synchronized (s_listBullet) {
                for (int i = s_listBullet.size() - 1; i >= 0 ; i--) {                    
                    if(((MyActor)s_listBullet.elementAt(i)).ISDie())
                    s_listBullet.removeElement(s_listBullet.elementAt(i));
                } 
            }
            
            //Remove all enemy is died
            synchronized (s_listEnemy) {
                for (int i = s_listEnemy.size() - 1; i >= 0 ; i--) {                    
                    if(((MyActor)s_listEnemy.elementAt(i)).ISDie())
                    s_listEnemy.removeElement(s_listEnemy.elementAt(i));
                } 
            }

        }
    }

    public void Render(Graphics g) {
        s_player.Render(g);

        synchronized (s_listBullet) {
            MyBullet bullet;
            for (int i = 0; i < s_listBullet.size(); i++) {
                bullet = (MyBullet) s_listBullet.elementAt(i);
                bullet.Render(g);
            }
        }

        synchronized (s_listEnemy) {
            for (int i = 0; i < s_listEnemy.size(); i++) {
                MyActor enemy = (MyActor) s_listEnemy.elementAt(i);
                enemy.Render(g);
            }
        }
    }
}
